#include <bits/stdc++.h>
using namespace std;
const int N = 5005;

int n, m, k;
int a[N], c[N];
int d[N][N];
int x[5];
int ans;

int solve(int x){
    int ret = 0;
    int powtmp = 1;
    int ktmp = 0;
    while(powtmp < x){
        powtmp = powtmp << 1;
        ktmp ++;
    }
    int yes[N];
    for(int i = 1; i <= powtmp; i++)
        yes[i] = 0;
    int rtmp = 0;
    queue<int> q;
    for(int i = 1; i <= x; i++)
        q.push(a[i]);
    while(powtmp){
        rtmp ++;
        for(int i = 1; i <= powtmp; i += 2){
            if(i + 1 <= x){
                int fir = i;
                int xfir = q.front();
                q.pop();
                int sec = i + 1;
                int yfir = q.front();
                q.pop();
                if(d[rtmp][fir]){
                    if(xfir >= rtmp)
                        q.push(xfir);
                    else
                        q.push(yfir);
                }
                else{
                    if(yfir >= rtmp)
                        q.push(xfir);
                    else
                        q.push(yfir);
                }    
            }
            else
                yes[i] = 1;
            cout << i << " " << yes[i] << endl;
        }
        powtmp = powtmp >> 1;
    }
    for(int i = 1; i <= powtmp; i++)
        if(yes[i])
            ret ++;
    return ret;
}

void work(){
    int res[N];
    int ret;
    for(int i = 1; i <= m; i++){
        int tot = c[i];
        res[i] = solve(tot);
    }
    ret = res[1];
    for(int i = 2; i <= m; i++)
        ret = ret ^ (i * res[i]);
    ans = ret;
}

int main(){
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= m; i++){
        cin >> c[i];
    }
    int powtmp = 1;
    k = 0;
    while(powtmp < n){
        powtmp = powtmp << 1;
        k ++;
    }
    memset(d, -1, sizeof d);
    for(int i = 1; i <= k; i++){
        for(int j = 1; j <= (1 << (k - i)); j++){
            char c;
            cin >> c;
            d[i][j] = c - '0';
        }
    }
    int T;
    cin >> T;
    while(T --){
        for(int i = 0;i < 4; i++)
            cin >> x[i];
        for(int i = 1; i <= n; i++)
            a[i] = a[i] ^ x[i % 4];
        work();
        cout << ans << endl;
    }

    return 0;
}